package com.wf.course.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mysql.jdbc.StringUtils;
import com.utils.page.PageBean;
import com.wf.course.dao.ICourseCommentMapper;
import com.wf.course.model.course.CourseComment;
import com.wf.course.service.ICourseCommentService;

@Service
@Transactional(rollbackFor=Exception.class)
public class ICourseCommentServiceImpl implements ICourseCommentService{
	
	@Autowired
	private ICourseCommentMapper mapper;

	@Override
	public void indexInfo(PageBean<CourseComment> pageBean) {
		// TODO Auto-generated method stub
		pageBean.setResults(this.mapper.queryList(pageBean));
		pageBean.setTotalCount(this.mapper.getTotalCount(pageBean));
	}

	@Override
	public void saveOrUpdateCourseComment(CourseComment courseComment) {
		// TODO Auto-generated method stub
		//添加
		if(courseComment.getId()==null){
			//创建时间
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			courseComment.setCommentTime(df.format(new Date()));
			courseComment.setRec_flag(2);
			this.mapper.saveCourseComment(courseComment);
		}else{
		 //修改
		if (!StringUtils.isNullOrEmpty(courseComment.getReply())) {
			  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			  courseComment.setReply_time(df.format(new Date()));
		  }
		  this.mapper.updateCourseComment(courseComment);
		  
		}
	}

	@Override
	public CourseComment queryCourseCommentById(Integer id) {
		// TODO Auto-generated method stub
		return this.mapper.queryCourseCommentById(id);
	}
		
	@Override
	public CourseComment queryAllCourseCommentById(Integer id) {
		// TODO Auto-generated method stub
		return this.mapper.queryAllCourseCommentById(id);
	}

	@Override
	public void deleteById(Integer id) {
		// TODO Auto-generated method stub
		this.mapper.deleteById(id);
	}
	@Override
	public boolean batchdeleteComment(String[] ids){
		return this.mapper.batchdeleteComment(ids);
	}
	@Override
	public boolean setTop(Integer id) {
		// TODO Auto-generated method stub
		boolean b = false;
		try {
			CourseComment courseComment = this.mapper.queryCourseCommentById(id);
			courseComment.setIsTop(1);
			this.mapper.updateCourseComment(courseComment);
			b = true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return b;
	}
	

}
